package com.yjck.ijpay.config.wxLogin;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxRuntimeException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

/**
 * @author <a href="https://github.com/binarywang">Binary Wang</a>
 */
@Slf4j
@Configuration
@EnableConfigurationProperties(WxMaProperties.class)
public class WxMaConfiguration {

    private final WxMaProperties properties;

    private static WxMaService maServices;

    @Autowired
    public WxMaConfiguration(WxMaProperties properties) {
        this.properties = properties;
    }

    public static WxMaService getMaService() {
        return maServices;
    }

    @PostConstruct
    public void init() {
        WxMaProperties a = this.properties;
        if (a == null) {
            throw new WxRuntimeException("大哥，拜托先看下项目首页的说明（readme文件），添加下相关配置，注意别配错了！");
        }
        WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();

        config.setAppid(a.getAppid());
        config.setSecret(a.getSecret());
        config.setToken(a.getToken());
        config.setAesKey(a.getAesKey());
        config.setMsgDataFormat(a.getMsgDataFormat());
        maServices = new WxMaServiceImpl();
        maServices.setWxMaConfig(config);
    }


}
